/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rules;

import types.ColoringInfo;
import types.ISudokuMatrix;
import java.util.Collection;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import utils.RuleHelper;



/**
 *
 * @author kostja
 */
public class RowTwinCandidateElimination implements Rule {
    int quadrat = 0;

    public void setQuadrat(int x, int y) throws Exception {
        this.quadrat = RuleHelper.calculateQuadratNumber(x, y);
    }

    public int getQuadrat() {
        return this.quadrat;
    }

    public String getName() {
        return RowTwinCandidateElimination.class.getSimpleName();
    }

    public String getDesc() {
        return "RowTwinCandidateElimination";
    }

    public boolean isApplicable(int row, int col, ISudokuMatrix matrix) {
        Collection targetFieldCandidats=RuleHelper.getFieldCandidateAfterCheckQuadratRowAndCol(row,col,matrix);
        Collection allEmptyColNeighbours=RuleHelper.getAllEmptyColNeighbours(row,col, matrix);
        Collection tempColNeighbours=RuleHelper.cloneCollection(allEmptyColNeighbours);
        Collection checkFieldCandidate;
        Collection checkFieldCandidate_1;
        int neighbourCheckNow;
        int neighbourCheckNow_1;
        Iterator iterOtherEmptyNeighbours;
        Iterator iterEmptyNeighbours=allEmptyColNeighbours.iterator();
        while(iterEmptyNeighbours.hasNext()){ // gehe alle leere Nachbarnfelder durch
            neighbourCheckNow=(Integer)(iterEmptyNeighbours.next());
            checkFieldCandidate=RuleHelper.getFieldCandidateAfterCheckQuadratRowAndCol(neighbourCheckNow, col, matrix);
            if(checkFieldCandidate.size()==2 && RuleHelper.containMinOneElement(targetFieldCandidats,checkFieldCandidate)){
                tempColNeighbours.remove(neighbourCheckNow); // alle anderen Nachbarn
                iterOtherEmptyNeighbours=tempColNeighbours.iterator();
                while(iterOtherEmptyNeighbours.hasNext()){
                    neighbourCheckNow_1=(Integer)iterOtherEmptyNeighbours.next();
                    checkFieldCandidate_1=RuleHelper.getFieldCandidateAfterCheckQuadratRowAndCol(neighbourCheckNow_1, col, matrix);
                    if(checkFieldCandidate_1.size()==2 && checkFieldCandidate.containsAll(checkFieldCandidate_1)){
                        targetFieldCandidats.removeAll(checkFieldCandidate_1);
                    }
                }
            }
        }
        if(targetFieldCandidats.size()==1){
            return true;
        }else
            return false;

    }

    public int getWeight(int QuadratNumber) {
        return 100;
    }

    public int compareTo(Rule o) {
        if (this.getWeight(this.getQuadrat())<o.getWeight(o.getQuadrat()))
            return -1;
        else if (this.getWeight(this.getQuadrat())>o.getWeight(o.getQuadrat()))
            return 1;
        else
            return 0;
    }

    public ColoringInfo getCheckedUnits(int x, int y, ISudokuMatrix matrix) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
